using System;
using System.Threading;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Proyecto_XNA
{
    class GameplayScreen : GameScreen
    {
        ContentManager content;
        SpriteFont gameFont;

        Player kPlayer;

        Vector2 playerPosition = new Vector2(200, 400);
        Vector2 enemyPosition = new Vector2(100, 100);

        string[] m_sMapName;
        string[] m_sRectName;

        private List<Rectangle> kRectangles = new List<Rectangle>();
        private Rectangle kRectangle;

        Level kLevel;

        Random random = new Random();

        public GameplayScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(1.0);
            
        }

        public override void LoadContent()
        {
            if (content == null)
            {
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            }

            gameFont = content.Load<SpriteFont>("SpriteFont/gamefont");

            kLevel = new Level();

            m_sMapName = new string[4];
            m_sRectName = new string[4];

            m_sMapName[0] = "Screen1";
            m_sRectName[0] = "FirstMapRect";
            m_sRectName[1] = "SecondMapRect";

            kLevel.FillMap(ScreenManager.Game.Services, m_sMapName[0]);

            kRectangle = new Rectangle(0, 0, 2327, 600);
            kLevel.FillRectangles(kRectangle, m_sRectName[0]);

            kRectangle = new Rectangle(0, 600, 2182, 600);
            kLevel.FillRectangles(kRectangle, m_sRectName[1]);

            kPlayer = new Player(kLevel, playerPosition);

            Thread.Sleep(1000);

            ScreenManager.Game.ResetElapsedTime();
        }

        public override void UnloadContent()
        {
            content.Unload();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {
                const float randomization = 10;

                enemyPosition.X += (float)(random.NextDouble() - 0.5) * randomization;
                enemyPosition.Y += (float)(random.NextDouble() - 0.5) * randomization;

                Vector2 targetPosition = new Vector2(200, 200);

                enemyPosition = Vector2.Lerp(enemyPosition, targetPosition, 0.05f);
            }

            if (!kPlayer.IsAlive)
            {
                kPlayer.ApplyPhysics(gameTime);
            }
            else
            {
                kPlayer.Update(gameTime);
            }
        }

        public override void HandleInput(InputState input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            int playerIndex = (int)ControllingPlayer.Value;

            KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];

            if (input.IsPauseGame(ControllingPlayer))
            {
                ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
            }
            else
            {
                Vector2 movement = Vector2.Zero;

                if (keyboardState.IsKeyDown(Keys.Left))
                {
                    movement.X--;
                }

                if (keyboardState.IsKeyDown(Keys.Right))
                {
                    movement.X++;
                }

                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    movement.Y--;
                }

                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    movement.Y++;
                }

                if (movement.Length() > 1)
                {
                    movement.Normalize();
                }

                playerPosition += movement * 2;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            
            ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.CornflowerBlue, 0, 0);

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin();

            kLevel.DrawMap(m_sMapName[0], m_sRectName[1], spriteBatch);
            kPlayer.Draw(gameTime, spriteBatch);

            spriteBatch.End();

            if (TransitionPosition > 0)
            {
                ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
            }
        }
    }
}
